import sys
import os
import pygame
import Numeric
import random
import re

re_dict = re.compile('.+?(\{.+?\}).+?')

class Video():
        def __init__(self,tp):
                pygame.init()
                pygame.event.set_allowed(None)
                self.size = tp
                self._ventana = pygame.display.set_mode(tp,0,32)
                self.array = pygame.surfarray.pixels2d(self._ventana)
                pygame.display.set_caption("Mandelbrot")
                self.instancia = 0

        def refresh(self):
                pygame.surfarray.blit_array(self._ventana, self.array)
                pygame.display.flip()
                self.save('imagen'+str(self.instancia)+'.jpg')
                self.instancia+=1
                return

        def refreshPalette(self):
                self.applyPalette()
                self.refresh()
                return
        
        def pintar(self,color):
                for x in range(self.size[0]):
                        for y in range(self.size[1]):
                                self.array[(x,y)]= int('ff0000',16)
                                self.array[(x,y)]= self.array[(x,y)] + int(color,16)
                self.refresh()
                return
        def applyPalette(self):
                pass                

        def finalizar(self):
                pygame.quit()
                return

        def waitForClick(self):
                clikeo = False
                mouse = pygame.MOUSEBUTTONDOWN
                tecla = pygame.KEYDOWN
                pygame.event.set_allowed(mouse)
                pygame.event.set_allowed(tecla)
                while not clikeo:
                        if pygame.event.peek(mouse):
                                evs = pygame.event.get(mouse)
                                for evento in evs:
                                        dicc = eval(re_dict.findall(str(evento))[0])
                                        x = dicc['pos'][0]
                                        y = dicc['pos'][1]
                                clikeo = True
                        if pygame.event.peek(tecla):
                            evento = pygame.event.poll()
                            dicc = eval(re_dict.findall(str(evento))[0])
                            if(dicc['key'] == 27):
                                return (-1,-1)
                pygame.event.set_allowed(None)
                pygame.event.clear()
                return (x,y)
        def getRect(self):
                clikeo = False
                mouse_click = pygame.MOUSEBUTTONDOWN
                mouse_movement = pygame.MOUSEMOTION
                pygame.event.set_allowed(mouse_click)
                while not clikeo:
                        if pygame.event.peek(mouse_click):
                                evs = pygame.event.get(mouse_click)
                                for evento in evs:
                                        dicc = eval(re_dict.findall(str(evento))[0])
                                        inicio_x = dicc['pos'][0]
                                        inicio_y = dicc['pos'][1]
                                clikeo = True
                pygame.event.set_allowed(mouse_movement)
                clikeo = False
                while not clikeo:
                        if pygame.event.peek(mouse_click):
                                evs = pygame.event.get(mouse_click)
                                for evento in evs:
                                        dicc = eval(re_dict.findall(str(evento))[0])
                                        fin_x = dicc['pos'][0]
                                        fin_y = dicc['pos'][1]
                                clikeo = True
                        else:
                            if pygame.event.peek(mouse_movement):
                                evs = pygame.event.get(mouse_movement)
                                dicc = eval(re_dict.findall(str(evs[-1]))[0])
                                xf = dicc['pos'][0]
                                yf = dicc['pos'][1]
                                self.dibujarRect(inicio_x,inicio_y,xf,yf)
                        pygame.event.clear()
                            
                pygame.event.clear()
                pygame.event.set_allowed(None)
                return (inicio_x, inicio_y),(fin_x, fin_y)

        def dibujarRect(self, xi, yi, xf, yf):
            if(xf < xi):
                temp = xf
                xf = xi
                xi = temp
            if(yf < yi):
                temp = yf
                yf = yi
                yi = temp
            zona = pygame.rect.Rect(xi,yi,xf-xi,yf-yi)
            pygame.color.Color(255,255,255,0)
            pygame.draw.rect(self._ventana, color, zona, 1)            
            self.refresh()
            return

        def save(self,archivo):
            pygame.image.save(self._ventana, archivo)
            return
        
                
            
        
                                        
